﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace AUV5.Web.Service
{
    public partial class MessageShow : PageBaseService
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int pageindex = 0;
            int pagesize = 0;
            try
            {
                pageindex = int.Parse(Request["pageindex"]??"0");
                pagesize = int.Parse(Request["pagesize"]??"0");
            }
            catch 
            {
                Response.Write("113");   //无效的整型
                return;
            }
            string userid = Request["userid"] ?? "";
            string contactuserid = Request["contactuserid"] ?? "";
            string secretkey=Request["secretkey"]??"";

            if (!IsValidKey(secretkey, pageindex, pagesize, userid, contactuserid))
            {
                Response.Write("112");//密钥不正确 
                return;
            }

            if (Request["pagesize"] != null && Request["pageindex"] != null && Request["userid"] != null && Request["contactuserid"] != null)
            {
                Model.UserInfo user = BLL.UserInfoBLL.Instance.GetDetail(userid);
                if (user == null)
                {
                    Response.Write("301");//无效的用户
                    return;
                }
                Model.UserInfo contactuser = BLL.UserInfoBLL.Instance.GetDetail(contactuserid);
                if (contactuser == null)
                {
                    Response.Write("301");//无效的用户
                    return;
                }
                int recordcount = 0;
                IList<Model.Message> list= BLL.MessageBLL.Instance.ShowMessage(userid, contactuserid, pagesize, pageindex, ref recordcount);
                
                Hashtable ht = new Hashtable();
                ht.Add("RecordCount",recordcount);
                ht.Add("MessageList", list);

                Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(ht));
            }
            else
            {
                Response.Write("111");//参数不正确
            }

        }

        private bool IsValidKey(string secretkey, int pageindex, int pagesize, string userid, string contactuserid)
        {
            if (secretkey_enable)
            {
                if (string.IsNullOrEmpty(secretkey))
                {
                    return false;
                }
                else
                {
                    string finalkey = base.EncryptMD5(public_key + current_day + pageindex.ToString() + pagesize.ToString() + userid + contactuserid).Substring(6,22) ;
                    return secretkey == finalkey;
                }
            }
            else
            {
                return true;    
            }
        }
    }
}